package com.live.dengdengtv.fragment.hall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.activity.support.CurLiveInfo;
import com.live.dengdengtv.activity.live.LiveShowAdapter;
import com.live.dengdengtv.activity.live.LiveingActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.gson.LiveInfoItem;
import com.live.dengdengtv.fragment.BaseFragment;
import com.live.dengdengtv.bean.Constants;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.Notice;
import com.live.dengdengtv.view.ImageCycleView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;

/**
 * Created by wyf on 2016/7/31.
 */
public class HotFragment extends BaseFragment {

    private static final String TAG = "HotFragment";
    private static HotFragment hotFragment = new HotFragment();

    private ImageCycleView mAdView;

    private ArrayList<String> mImageUrl = null;

    private String imageUrl1 = "http://img.zcool.cn/community/01640855531d260000009af0a958a5.jpg";
//    private String imageUrl1 = "http://img381.ph.126.net/uTP90dPhJFBAX-ko-KklXg==/2388315177391143143.jpg";

        private String imageUrl2 = "http://img6.pplive.cn/2013/06/22/18043077374.jpg";
//        private String imageUrl2 = "http://img.zcool.cn/community/0110b756299f256ac72548787b3394.jpg";
//    private String imageUrl2 = "http://img1.imgtn.bdimg.com/it/u=2726530206,4001853203&fm=15&gp=0.jpg";

//    private String imageUrl3 = "http://imgs.xiuna.com/xiezhen/2013-3-20/1/12.jpg";
//    private String imageUrl3 = "http://www.xiying.com/16_z/z4/z4635/009.jpg";
    private String imageUrl3 = "http://img.zcool.cn/community/016e5d56299e5e32f8755701820b33.jpg";

    private ArrayList<LiveInfoItem> liveList = new ArrayList<LiveInfoItem>();
    private LiveShowAdapter adapter;
    private ListView mLiveList;
    private SwipeRefreshLayout swipeLayout;

    private View contentView;

    public static HotFragment getInstance() {
        return hotFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if(contentView == null) {
            contentView = inflater.inflate(R.layout.frag_hot, container, false);


            mImageUrl = new ArrayList<String>();
            mImageUrl.add(imageUrl1);
            mImageUrl.add(imageUrl2);
            mImageUrl.add(imageUrl3);

            LinearLayout hall_loopview = (LinearLayout) inflater.inflate(R.layout.hall_loopview, null);
            mAdView = (ImageCycleView) hall_loopview.findViewById(R.id.ad_view);
            mAdView.setImageResources(mImageUrl, mAdCycleViewListener);

            mLiveList = (ListView) contentView.findViewById(R.id.live_lv);
            mLiveList.addHeaderView(hall_loopview);

            adapter = new LiveShowAdapter(getActivity(), R.layout.live_single, liveList);
            mLiveList.setAdapter(adapter);

            mLiveList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    LiveInfoItem item = liveList.get(i - 1);
                    //如果是自己
                    if (item.getCreator_id().equals(LoginInfo.getInstance().getUserId())) {
//                    Intent intent = new Intent(getActivity(), LiveActivity.class);
                        Intent intent = new Intent(getActivity(), LiveingActivity.class);
                        intent.putExtra(Constants.ID_STATUS, Constants.HOST);
                        CurLiveInfo.setIdStatus(Constants.HOST);
                        CurLiveInfo.setJoinRoomWay(false);
                        CurLiveInfo.setHostID(item.getCreator_id());
                        CurLiveInfo.setHostName(item.getCreator_nickname());
                        CurLiveInfo.setAvatorurl(item.getCover());
                        CurLiveInfo.setRoomNum(Integer.parseInt(item.getLive_room_id()));
                        CurLiveInfo.setMembers(Integer.parseInt(item.getCount_of_looker())); // 添加自己
                        CurLiveInfo.setAdmires(Integer.parseInt(item.getCount_of_admire()));
                        CurLiveInfo.setAddress("北京");
                        CurLiveInfo.setLiveId(item.getId());
                        startActivity(intent);
                    } else {
//                    Intent intent = new Intent(getActivity(), LiveActivity.class);
                        Intent intent = new Intent(getActivity(), LiveingActivity.class);
                        intent.putExtra(Constants.ID_STATUS, Constants.MEMBER);
                        CurLiveInfo.setIdStatus(Constants.MEMBER);
                        CurLiveInfo.setJoinRoomWay(false);
                        CurLiveInfo.setHostID(item.getCreator_id());
                        CurLiveInfo.setHostName(item.getCreator_nickname());
                        CurLiveInfo.setAvatorurl(item.getCover());
                        CurLiveInfo.setRoomNum(Integer.parseInt(item.getLive_room_id()));
//                        CurLiveInfo.setRoomNum(940);
                        CurLiveInfo.setMembers(Integer.parseInt(item.getCount_of_looker()) + 1); // 添加自己
                        CurLiveInfo.setAdmires(Integer.parseInt(item.getCount_of_admire()));
                        CurLiveInfo.setAddress("北京");
                        CurLiveInfo.setLiveId(item.getId());
                        startActivity(intent);
                    }

                }
            });


            swipeLayout = (SwipeRefreshLayout) contentView.findViewById(R.id.swipe_container);
            swipeLayout.setColorSchemeResources(R.color.app_red);
//                    android.R.color.holo_orange_dark);
//                    android.R.color.holo_green_light,
//                    android.R.color.holo_orange_light,
//                    android.R.color.holo_red_light);
            swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {

                    getPageData();//刷新页面

                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            swipeLayout.setRefreshing(false);
                        }
                    }, 5000);
                }
            });
        }
        return contentView;
    }





    private ImageCycleView.ImageCycleViewListener mAdCycleViewListener = new ImageCycleView.ImageCycleViewListener() {

        @Override
        public void onImageClick(int position, View imageView) {
            // TODO 单击图片处理事件
            Toast.makeText(getContext(), "position->"+position, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void displayImage(String imageURL, ImageView imageView) {
            ImageLoader.getInstance().displayImage(imageURL, imageView);// 此处本人使用了ImageLoader对图片进行加装！
        }
    };


    private void getPageData(){

        HashMap<String,String> params = new HashMap<String,String>();
        params.put("page","1");
        params.put("page_count","20");
        params.putAll(AppInfo.getHeadInfo());
        requestNetData(NetTaskList.live_list, params, 1, false);
//        NetTaskList.getInstance().getLiveList(getActivity(), 1,this);
    }


    @Override
    public void onError(Call call, Exception e, int id) {
        Notice.alert(getActivity(), e.getMessage());
        swipeLayout.setRefreshing(false);
    }
    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        liveList.clear();
        if(response.result == 0){
            JsonArray array = response.data.getAsJsonArray();
            for (int i=0; i<array.size() ;i++){
                LiveInfoItem liveItem = new Gson().fromJson(array.get(i).getAsJsonObject(), LiveInfoItem.class);
                liveList.add(liveItem);
            }
        }
        adapter.notifyDataSetChanged();
        swipeLayout.setRefreshing(false);
        return false;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        getPageData();
    }

    @Override
    public void onStart() {
        super.onStart();
    }
    @Override
    public void onResume() {
        super.onResume();
        mAdView.startImageCycle();
    };

    @Override
    public void onPause() {
        super.onPause();
        mAdView.pushImageCycle();
    }

    @Override
    public void onDestroy() {
        mAdView.pushImageCycle();
        super.onDestroy();
    }
}
